package com.example.shardingjdbc.service.impl;


import java.math.BigDecimal;
import java.util.Date;

import com.alibaba.fastjson2.JSON;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.example.shardingjdbc.mapper.MemberMapper;
import com.example.shardingjdbc.model.DictTable;
import com.example.shardingjdbc.model.Member;
import com.example.shardingjdbc.model.Order;
import com.example.shardingjdbc.model.OrderItem;
import com.example.shardingjdbc.service.OrderService;
import com.example.shardingjdbc.shardingMapper.DictTableMapper;
import com.example.shardingjdbc.shardingMapper.OrderItemMapper;
import com.example.shardingjdbc.shardingMapper.OrderMapper;
import com.example.shardingjdbc.utils.IdWorker;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
	@Autowired
	private OrderMapper orderMapper;
	@Autowired
	private OrderItemMapper orderItemMapper;
	@Autowired
	private MemberMapper memberMapper;
	@Autowired
	private DictTableMapper dictTableMapper;
	
	private static long member_Id = 12434324233l;

	@DSTransactional
	public String insertOrder() {
		Member member = memberMapper.selectByPrimaryKey(2l);
		log.info("member:{}", JSON.toJSONString(member));
		DictTable dictTable = new DictTable();
		dictTable.setId(3);
		dictTable.setName("南昌");
		dictTable.setValue("省会");
		dictTableMapper.insert(dictTable);

		Order record = new Order();
		long orderId = IdWorker.getLongId();
		record.setOrderId(orderId);
		record.setCreatedDate(new Date());
		record.setDeleteFlag("0");
		record.setMemberId(member_Id);
		record.setStatus("gc100");
		record.setTotalAccount(new BigDecimal("123.00"));
		orderMapper.insertSelective(record);
		
		OrderItem item1 = new OrderItem();
		long itemId = IdWorker.getLongId();
		item1.setItemId(itemId);
		item1.setCreatedDate(new Date());
		item1.setItemAccount(110l);
		item1.setMemberId(member_Id);
		item1.setProductName("狗皮膏药");
		item1.setOrderId(record.getOrderId());
		orderItemMapper.insertSelective(item1);
		
		JSONObject a = new JSONObject();
		JSONArray arr = new JSONArray();
		arr.add(item1);
		a.put("order", record);
		a.put("items", arr);
		return a.toJSONString();
	}

}
